{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d9df37ee",
   "metadata": {},
   "source": [
    "# 复习while"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69ed2f5d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 人：1/2，1/3，原点，路的长度是1，随机打开一个门\n",
    "import random"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df9342d2",
   "metadata": {},
   "source": [
    "# IF "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e667dd06",
   "metadata": {},
   "source": [
    "## 单支"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "28125faf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a>b\n"
     ]
    }
   ],
   "source": [
    "a=8\n",
    "b=5\n",
    "if a>b:\n",
    "    print('a>b')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df49956c",
   "metadata": {},
   "source": [
    "## 双分支"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f0c0a211",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a≤b\n"
     ]
    }
   ],
   "source": [
    "a=5\n",
    "b=5\n",
    "if a>b:\n",
    "    print('a>b')\n",
    "else:\n",
    "    print('a≤b')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16f07394",
   "metadata": {},
   "source": [
    "## 多分支"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "907e4fdf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a＜b\n"
     ]
    }
   ],
   "source": [
    "a=-1\n",
    "b=0\n",
    "if a>b:\n",
    "    print('a>b')\n",
    "elif a==b:\n",
    "    print('a=b')\n",
    "else:\n",
    "    print('a＜b')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2fcca391",
   "metadata": {},
   "source": [
    "## IF 练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9d7eaa71",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "4\n",
      "2\n",
      "不是\n"
     ]
    }
   ],
   "source": [
    "a,b,c=int(input()),int(input()),int(input()) # 必须为正\n",
    "# 第一种写法\n",
    "if a>0 and b>0 and c>0: \n",
    "    if a+b<=c:\n",
    "        print('不是')\n",
    "    elif a+c<=b:\n",
    "        print('不是')\n",
    "    elif b+c<=a:\n",
    "        print('不是')\n",
    "    else:\n",
    "        print('是三角形')\n",
    "else:\n",
    "    print('是三角形')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "2735b009",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-3\n",
      "-5\n",
      "-4\n",
      "不是\n"
     ]
    }
   ],
   "source": [
    "a,b,c=int(input()),int(input()),int(input())\n",
    "if a+b>c and a+c>b and b+c>a:\n",
    "    print('是的')\n",
    "else:\n",
    "    print('不是')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "54407666",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不是\n"
     ]
    }
   ],
   "source": [
    "if a+b+c-max([a,b,c])>max([a,b,c]):\n",
    "    print('是的')\n",
    "else:\n",
    "    print('不是')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "e556e792",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9, 25, 49, 2025]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list1=[3,5,7,32,78,96,45,28]\n",
    "# 求奇数的平方\n",
    "new_list1=[]\n",
    "for i in list1:\n",
    "    if i%2==1:\n",
    "        new_list1.append(i**2)\n",
    "new_list1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "68d8853c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['202221101173', '202221101174', '202221101176', '202221101177']"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list2=[\"201821102035:23\",\n",
    "\"201821102084:12\",\n",
    "\"202121132142:42\",\n",
    "\"202221092046:45\",\n",
    "\"202221101173:73\",\n",
    "\"202221101174:81\",\n",
    "\"202221101175:24\",\n",
    "\"202221101176:95\",\n",
    "\"202221101177:100\"] # 找出及格的同学\n",
    "New_list2=[]\n",
    "# 对所有人的成绩循环\n",
    "for i in list2:\n",
    "    # 学号成绩分开\n",
    "    pearson_list=i.split(':')\n",
    "    #提取成，整数,判断\n",
    "    if int(pearson_list[-1])>=60:\n",
    "        # 数据录入\n",
    "        New_list2.append(pearson_list[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0720632b",
   "metadata": {},
   "source": [
    "\n",
    "# 列表推导式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "ef348e68",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 4, 6, 8]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list4=[]\n",
    "for i in range(0,10,2):\n",
    "    list4.append(i)\n",
    "list4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "abc75ad4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 4, 6, 8]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list3=[i for i in range(0,10,2)]\n",
    "list3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "6bcab295",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "9\n",
      "25\n",
      "49\n",
      "81\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[1, 9, 25, 49, 81]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 利用列表推导式:1,3,5,7,9的平方:[1,9,25,49,81]\n",
    "for i in range(1,10,2):\n",
    "    print(i**2)\n",
    "list2=[i**2 for i in range(1,10,2)]\n",
    "list2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "e0aa549f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9, 25, 49, 2025]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list1=[3,5,7,32,78,96,45,28]\n",
    "# 求奇数的平方\n",
    "new_list1=[]\n",
    "for i in list1:\n",
    "    if i%2==1:\n",
    "        new_list1.append(i**2)\n",
    "\n",
    "New_list3=[i**2 for i in list1 if i%2==1]\n",
    "New_list3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38db89e0",
   "metadata": {},
   "source": [
    "# pass,break,continue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "58cb0edf",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(10):\n",
    "    ...\n",
    "for i in range(10):\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "0cb8fc48",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "break之前\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "for i in range(3):\n",
    "    print('break之前')\n",
    "    print(i)\n",
    "    break  # 终止整个循环\n",
    "    print('break之后')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "fa7c589f",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "continue之前\n",
      "0\n",
      "continue之前\n",
      "1\n",
      "continue之前\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "for i in range(3):\n",
    "    print('continue之前')\n",
    "    print(i)\n",
    "    continue  # 中止当前循环后面的语句,不影响大的循环\n",
    "    print('continue之后')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "id": "ae56f285",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 1.3333333333333333, [1, 0, 0])"
      ]
     },
     "execution_count": 160,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 练习\n",
    "\n",
    "# 人：1/2，1/3，原点，路的长度是1，随机打开一个门\n",
    "import random\n",
    "door=[1/2,1/3,0] # 记录门后的路\n",
    "lu=0 # 记录走过的路的长度\n",
    "CD=[]  # 记录每次的选择\n",
    "while True: # 不断循环\n",
    "    c_d=random.randint(0,2) # 选择门\n",
    "    CD.append(c_d)  # 记录选择\n",
    "    # 根据门 修改路程\n",
    "    if c_d==2:\n",
    "        lu=0\n",
    "    else:\n",
    "        lu+=door[c_d]\n",
    "    # 判断是否出去了\n",
    "    if lu>=1:\n",
    "        break\n",
    "len(CD),lu,CD    # "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "362e28d6",
   "metadata": {},
   "source": [
    "# 函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "id": "1588861a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def name():  # def XXXX():   规定\n",
    "    ...  # 函数内部的信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 186,
   "id": "3146906d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开心\n"
     ]
    }
   ],
   "source": [
    "def name():\n",
    "    print('😊')  # 没有返回值,无法接收结果\n",
    "a=name()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 188,
   "id": "f83a2d47",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'😊'"
      ]
     },
     "execution_count": 188,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def name():\n",
    "    return '😊'  # 有了return 才能返回结果,参数才有东西接受\n",
    "a=name()\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 192,
   "id": "093b1fe6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'随便,你好😊'"
      ]
     },
     "execution_count": 192,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def name(x): \n",
    "    return f'{x},你好😊'\n",
    "a=name('随便')  # x='9527', x参与函数的计算\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 193,
   "id": "1ba7d668",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "14"
      ]
     },
     "execution_count": 193,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def name(x): \n",
    "    return x**2+2*x-1\n",
    "name(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 208,
   "id": "c1d261c7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'不是三角形'"
      ]
     },
     "execution_count": 208,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def fun(x):\n",
    "    a,b,c=x\n",
    "    if a+b>c and a+c>b and b+c>a:\n",
    "        return '三角形'\n",
    "    else:\n",
    "        return '不是三角形'\n",
    "    \n",
    "s=fun([8,2,3])\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 209,
   "id": "dd868d44",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 209,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def  fun3(x,y):  #矩形的面积\n",
    "    return x*y\n",
    "fun3(3,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 222,
   "id": "712ef4cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1296"
      ]
     },
     "execution_count": 222,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def fun3(x,y,z):  #三个数连乘的积\n",
    "    return (x*y)**z\n",
    "fun3(3,2,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 217,
   "id": "7a1943e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'中南民族大学的莫言'"
      ]
     },
     "execution_count": 217,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def fun3(name,school='中南民族大学'):  #指定参数,默认参数\n",
    "    return f'{school}的{name}'\n",
    "fun3('莫言')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 218,
   "id": "88c1cc93",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'西南民族大学的莫言'"
      ]
     },
     "execution_count": 218,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fun3('莫言',school='西南民族大学')  # 修改已经定义的默认参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 225,
   "id": "fa718983",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1296"
      ]
     },
     "execution_count": 225,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def fun3(x,y,z):  #通过参数指定输入值时,位置可以随便变动\n",
    "    return (x*y)**z\n",
    "fun3(x=3,z=4,y=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 230,
   "id": "6d6d6fff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5050"
      ]
     },
     "execution_count": 230,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 给定一个数,求1+2+3+...+这个数\n",
    "def he(x):\n",
    "    list1=[i for i in range(1,x+1)]\n",
    "    return sum(list1)\n",
    "\n",
    "def he1(x):\n",
    "    ans=0\n",
    "    for i in range(1,x+1):\n",
    "        ans+=i\n",
    "    return ans\n",
    "he1(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 239,
   "id": "d121e68a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5050"
      ]
     },
     "execution_count": 239,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fun3333=lambda x:sum([i for i in range(1,x+1)])\n",
    "fun3333(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 237,
   "id": "1ce814b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "54"
      ]
     },
     "execution_count": 237,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fun=lambda x,y,z: x+y+z\n",
    "fun(17,18,19)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 241,
   "id": "324ccb5d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16"
      ]
     },
     "execution_count": 241,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求任意数x的y次方\n",
    "fun4=lambda x,y:x**y\n",
    "fun4(2,4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "806b6ce8",
   "metadata": {},
   "source": [
    "# 模块"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95071d7d",
   "metadata": {},
   "source": [
    "## 内置模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 244,
   "id": "8b38e2e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 243,
   "id": "d7467456",
   "metadata": {},
   "outputs": [],
   "source": [
    "import keyword"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 245,
   "id": "94da85a1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['.ipynb_checkpoints', 'DAY2.ipynb']"
      ]
     },
     "execution_count": 245,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "os.listdir()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d97e95c8",
   "metadata": {},
   "source": [
    "## 第三方模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 246,
   "id": "c44ef860",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 247,
   "id": "bb536cff",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 248,
   "id": "76e4cdfd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69f9881d",
   "metadata": {},
   "source": [
    "## 自己打包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 249,
   "id": "db092a11",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "文件正常导入\n",
      "文件导入完毕\n"
     ]
    }
   ],
   "source": [
    "import good_good_study"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 253,
   "id": "70af0132",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5050"
      ]
     },
     "execution_count": 253,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方便把自己的函数进行打包,可以重复调用\n",
    "good_good_study.he1(100)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 256,
   "id": "876e19ce",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-27T03:01:00.367865Z",
     "start_time": "2023-06-27T03:01:00.361813Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5050"
      ]
     },
     "execution_count": 256,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import testcode\n",
    "testcode.he1(100)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "165px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
